package com.xhs.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.xhs.entity.User;
import com.xhs.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	

	@RequestMapping("/login")
	@ResponseBody
	public Map<String, String> login(User user){
		User item = userService.getByUserName(user.getUserName());
		Map<String, String> map = new HashMap<String, String>();
		if (item!=null & item.getPassWord().equals(user.getPassWord())) {
			map.put("message", "success");
		}else {
			map.put("message", "fail");
		}
		return map;
	}
	
	/**
	 * 获取所有用户列表
	 * @param user
	 * @return
	 */
	@RequestMapping("/getAllUser")
	public ModelAndView getAllUser(ModelAndView model, Page<User> pager, User user){
		//设置pagetSize 
		pager.setPageSize(5);
		List<User> list = userService.query(pager,user);
		model.addObject("userList", list);
		model.addObject("pager", new PageInfo<User>(list));
		model.setViewName("/allUser");
		return model;
	}
	
	/**
	 * 跳转到添加用户界面
	 * @param request
	 * @return
	 */
	@RequestMapping("/toAddUser")
	public String toAddUser(HttpServletRequest request){
		return "/addUser";
	}
	
	/**
	 * 添加用户并重定向
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/addUser")
	public String addUser(User user,HttpServletRequest request){
		user.setCreateTime(new Date());
		boolean result = userService.add(user);
		if (result) {
			return "redirect:/user/getAllUser";
		}else {
			return "/error";
		}
		
	}
	
	/**
	 * 编辑用户
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateUser")
	public String updateUser(User user,HttpServletRequest request){
		user.setCreateTime(new Date());
		if(userService.update(user)){
			user = userService.getById(user.getId());
			request.setAttribute("user", user);
			return "redirect:/user/getAllUser";
		}else{
			return "/error";
		}
	}
	
	/**
	 * 根据id查询单个用户
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getUser")
	public String getUser(int id,HttpServletRequest request){
		request.setAttribute("user", userService.getById(id));
		return "/editUser";
	}
	
	/**
	 * 删除用户
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delUser")
	public void delUser(int id,HttpServletRequest request,HttpServletResponse response){
		String result = "{\"result\":\"error\"}";
		if(userService.delete(id)){
			result = "{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			PrintWriter out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
}
